package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.DmcCMbedsideBindMapper;
import com.csun.cmny.provider.dao.DmcbedsideBindMapper;
import com.csun.cmny.provider.exceptions.DmcBizException;
import com.csun.cmny.provider.model.domain.DmcBedsideCareBind;
import com.csun.cmny.provider.model.domain.DmcCMBedsideCareBind;
import com.csun.cmny.provider.model.domain.DmcDevice;
import com.csun.cmny.provider.model.dto.bedsideScreen.BindBedsideScreenDto;
import com.csun.cmny.provider.model.dto.bedsideScreen.BindCMBedsideScreenDto;
import com.csun.cmny.provider.model.dto.propertyManagement.SelectDeviceDataDto;
import com.csun.cmny.provider.model.vo.propertyManagement.PropertyDataVo;
import com.csun.cmny.provider.service.DmcBedsideScreenBindService;
import com.csun.cmny.provider.service.DmcDeviceService;
import com.csun.cmny.util.PublicUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.util.List;

import static com.csun.cmny.base.enums.ErrorCodeEnum.*;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-02-13 15:51
 * @Description: the class dmc sleep care bind service impl
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DmcBedsideBindServiceImpl extends BaseService<DmcBedsideCareBind>
        implements DmcBedsideScreenBindService {

    @Resource
    private DmcDeviceService dmcDeviceService;


    @Resource
    private DmcbedsideBindMapper dmcbedsideBindMapper;

    @Resource
    private DmcCMbedsideBindMapper dmcCMbedsideBindMapper;





    @Override
    public Integer bind(BindBedsideScreenDto bindBedsideScreenDto, LoginAuthDto loginAuthDto) throws DmcBizException {
        // 1.校验床头屏
        DmcDevice dmcDevice = dmcDeviceService.checkDeviceIsBedsideCareByDeviceId(bindBedsideScreenDto.getDeviceId());
        //校验床位是否绑定过
        DmcBedsideCareBind dmcBedsideCareBind1 = new DmcBedsideCareBind();
        DmcBedsideCareBind dmcBedsideCareBind2 = new DmcBedsideCareBind();
        BeanUtils.copyProperties(bindBedsideScreenDto, dmcBedsideCareBind1);
        dmcBedsideCareBind2.setBedId(dmcBedsideCareBind1.getBedId());

        dmcBedsideCareBind1 = dmcbedsideBindMapper.selectOne(dmcBedsideCareBind2);

        if(PublicUtil.isNotEmpty(dmcBedsideCareBind1)){
            throw new DmcBizException(DMC100611004,bindBedsideScreenDto.getBedId());
        }

        if (PublicUtil.isEmpty(dmcDevice)) {
            throw new DmcBizException(DMC10068007, bindBedsideScreenDto.getDeviceId());
        }

        if(PublicUtil.isEmpty(bindBedsideScreenDto.getLocation())){
            throw new DmcBizException(DMC100611002);
        }
        if(PublicUtil.isEmpty(bindBedsideScreenDto.getBedId())){
            throw new DmcBizException(DMC100611003);
        }


        // 2.校验床头屏是否已经被绑定
        DmcBedsideCareBind dmcBedsideCareBindQuery = dmcbedsideBindMapper.selectByPrimaryKey(bindBedsideScreenDto.getDeviceId());

        if (PublicUtil.isNotEmpty(dmcBedsideCareBindQuery)) {
            throw new DmcBizException(DMC10068008, dmcBedsideCareBindQuery.getDeviceId(),
                    dmcBedsideCareBindQuery.getLocation());
        }





        // 6.添加绑定


        DmcBedsideCareBind dmcBedsideCareBind = new DmcBedsideCareBind();
        BeanUtils.copyProperties(bindBedsideScreenDto, dmcBedsideCareBind);
        dmcBedsideCareBind.setUpdateInfo(loginAuthDto);


        return dmcbedsideBindMapper.insertSelective(dmcBedsideCareBind );
    }

    @Override
    public Integer bindWithRoom(BindCMBedsideScreenDto bindCMBedsideScreenDto, LoginAuthDto loginAuthDto) throws DmcBizException {
        // 校验床头屏
        DmcDevice dmcDevice = dmcDeviceService.checkDeviceIsBedsideCareByDeviceId(bindCMBedsideScreenDto.getDeviceId());
        if (PublicUtil.isEmpty(dmcDevice)) {
            throw new DmcBizException(DMC10068007, bindCMBedsideScreenDto.getDeviceId());
        }
        //校验床头屏是否是否已经被绑定
        DmcCMBedsideCareBind dmcCMBedsideCareBind3 = dmcCMbedsideBindMapper.selectByPrimaryKey(bindCMBedsideScreenDto.getDeviceId());
        if(PublicUtil.isNotEmpty(dmcCMBedsideCareBind3)){
            throw new DmcBizException(DMC10068008, dmcCMBedsideCareBind3.getDeviceId(),
                    dmcCMBedsideCareBind3.getRoomName());
        }

        //校验房间号是否绑定过
        DmcCMBedsideCareBind dmcCMBedsideCareBind1 = new DmcCMBedsideCareBind();
        dmcCMBedsideCareBind1.setBuildId(bindCMBedsideScreenDto.getBuildId());
        dmcCMBedsideCareBind1.setRoomName(bindCMBedsideScreenDto.getRoomName());
        DmcCMBedsideCareBind dmcCMBedsideCareBind2 = dmcCMbedsideBindMapper.selectOne(dmcCMBedsideCareBind1);
        if(PublicUtil.isNotEmpty(dmcCMBedsideCareBind2)){
            throw new DmcBizException(DMC100611005,bindCMBedsideScreenDto.getRoomName());
        }

        //添加绑定
        DmcCMBedsideCareBind dmcCMBedsideCareBind = new DmcCMBedsideCareBind();
        BeanUtils.copyProperties(bindCMBedsideScreenDto,dmcCMBedsideCareBind);
        dmcCMBedsideCareBind.setUpdateInfo(loginAuthDto);

        return dmcCMbedsideBindMapper.insertSelective(dmcCMBedsideCareBind);

    }

    @Override
    public Integer breakSleepCareWithRomm(Long deviceId) {
        // 1.判断设备Id是否存在
        DmcDevice dmcDevice = dmcDeviceService.checkDeviceIsBedsideCareByDeviceId(deviceId);
        if (PublicUtil.isEmpty(dmcDevice)) {
            throw new DmcBizException(DMC10068011, deviceId);
        }

        // 2.判断床头屏是否已经绑定
        DmcCMBedsideCareBind dmcCMBedsideCareBind = dmcCMbedsideBindMapper.selectByPrimaryKey(deviceId);
        if (PublicUtil.isEmpty(dmcCMBedsideCareBind)) {
            throw new DmcBizException(DMC10068012, deviceId);
        }

        // 3.解绑床头屏
        return dmcCMbedsideBindMapper.deleteByPrimaryKey(deviceId);
    }

    @Override
    public Integer breakSleepCare(Long deviceId) {
        // 1.判断设备Id是否存在
        DmcDevice dmcDevice = dmcDeviceService.checkDeviceIsBedsideCareByDeviceId(deviceId);
        if (PublicUtil.isEmpty(dmcDevice)) {
            throw new DmcBizException(DMC10068011, deviceId);
        }

        // 2.判断床头屏是否已经绑定
        DmcBedsideCareBind dmcSleepCareBindQuery = dmcbedsideBindMapper
                .selectByPrimaryKey(deviceId);
        if (PublicUtil.isEmpty(dmcSleepCareBindQuery)) {
            throw new DmcBizException(DMC10068012, deviceId);
        }

        // 3.解绑床头屏
        return dmcbedsideBindMapper.deleteByPrimaryKey(deviceId);

    }

    @Override
    public Integer deleteCsunBedsideById(Long id) {
        // 1.判断设备Id是否存在
        DmcDevice dmcDevice = dmcDeviceService.checkDeviceIsBedsideCareByDeviceId(id);
        if (PublicUtil.isEmpty(dmcDevice)) {
            throw new DmcBizException(DMC10068010, id);
        }

        // 2.判断床头屏是否已经绑定
        DmcBedsideCareBind dmcBedsideCareBind = dmcbedsideBindMapper
                .selectByPrimaryKey(id);
        if (PublicUtil.isNotEmpty(dmcBedsideCareBind)) {
            dmcbedsideBindMapper.deleteByPrimaryKey(id);
        }

        // 3.删除设备
        return dmcDeviceService.deleteDmcDeviceById(id);
    }

    @Override
    public List<PropertyDataVo> selectDeviceData(SelectDeviceDataDto selectDeviceDataDto) {
        return dmcbedsideBindMapper.selectDeviceData(selectDeviceDataDto);
    }
}
